home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
news
/
readers
/
skim-0.8
/
skim-0
/
skim-0.8.4
/
VarBufTest.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-18
|
11KB
|
424 lines
/*
* NAME
* VarBufTest.c
* DESCRIPTION
* Test the VarBuf ADT. Run this test by typing 'make test' in the source
* directory of skim.
* COPYRIGHT
* VarBuf - Variable size buffer ADT.
* Copyright (C) 1996 Rene W.J. Pijlman
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* VERSION
* $Header: /home/rene/sys/CVS_MasterSourceRepository/skim/VarBufTest.c,v 1.7 1996/02/18 11:40:25 rene Exp $
* Distributed with Skim version 0.8.4.
*/
#include "VarBuf.h"
#include "StandardIO.h"
#include <assert.h>
#include <string.h>
/* assert()'s must be in effect in this file. */
#ifdef NDEBUG
# undef NDEBUG
#endif
static void TestCreation( VarBuf * Buf1, VarBuf * Buf2 )
{
VarBuf Temp = VBCreateString("ABC");
VBDestroy( VBCreate() );
*Buf1 = VBCreate();
*Buf2 = VBCreate();
assert( !strcmp( VBAsString(Temp), "ABC" ) );
assert( VBSize(*Buf1) == 0 );
assert( VBSize(*Buf2) == 0 );
assert( strlen( VBAsString(*Buf1) ) == 0 );
assert( strlen( VBAsString(*Buf2) ) == 0 );
VBDestroy( Temp );
}
static void TestAppendStringAndChar( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBReset( Buf2 );
VBAppendString( Buf1, "Alle" );
VBAppendCharacter( Buf1, ' ' );
VBAppendString( Buf2, "eendjes" );
VBAppendVB( Buf1, Buf2 );
VBAppendCharacter( Buf1, ' ' );
VBAppendString( Buf1, "zwemmen" );
VBAppendCharacter( Buf1, ' ' );
VBReset( Buf2 );
VBAppendString( Buf2, "in" );
VBAppendCharacter( Buf2, ' ' );
VBAppendString( Buf2, "het" );
VBAppendVB( Buf1, Buf2 );
VBAppendCharacter( Buf1, ' ' );
VBAppendString( Buf1, "water" );
assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
}
static void TestTruncate( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBAppendString( Buf1, "ABC" );
VBTruncate( Buf1, 1 );
assert( VBSize(Buf1) == 1 );
assert( !strcmp(VBAsString(Buf1), "A"));
VBReset( Buf1 );
VBAppendString( Buf1, "ABC" );
VBTruncate( Buf1, 0 );
assert( VBSize(Buf1) == 0 );
assert( !strcmp(VBAsString(Buf1), ""));
VBReset( Buf2 );
VBAppendString( Buf2, "ABC" );
VBTruncate( Buf2, 3 );
assert( VBSize(Buf2) == 3 );
assert( !strcmp(VBAsString(Buf2), "ABC"));
}
#define HUGE 100000
static void TestManyAppends( VarBuf Buf1, VarBuf Buf2 )
{
int i;
VBReset( Buf1 );
for ( i = 0; i < HUGE; i++ )
{
VBAppendCharacter( Buf1, 'x' );
}
assert( VBSize( Buf1 ) == HUGE );
assert( strlen( VBAsString( Buf1 ) ) == HUGE );
for ( i = 0; i < HUGE; i++ )
{
assert( VBAsString(Buf1)[i] == 'x' );
}
}
static void TestShiftLeft( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBAppendString( Buf1, "ABC" );
VBShiftLeft(Buf1, 2);
assert( VBSize(Buf1) == 1 );
assert( !strcmp(VBAsString(Buf1), "C"));
VBReset( Buf1 );
VBAppendString( Buf1, "ABC" );
VBShiftLeft(Buf1, 3);
assert( VBSize(Buf1) == 0 );
assert( !strcmp(VBAsString(Buf1), ""));
VBReset( Buf2 );
VBAppendString( Buf2, "ABC" );
VBShiftLeft(Buf2, 0);
assert( VBSize(Buf2) == 3 );
assert( !strcmp(VBAsString(Buf2), "ABC"));
}
static void TestShiftRight( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBAppendString( Buf1, "ABC" );
VBShiftRight(Buf1, 2, '>' );
assert( VBSize(Buf1) == 5 );
assert( !strcmp(VBAsString(Buf1), ">>ABC"));
VBReset( Buf1 );
VBShiftRight(Buf1, 3, '>' );
assert( VBSize(Buf1) == 3 );
assert( !strcmp(VBAsString(Buf1), ">>>"));
VBReset( Buf2 );
VBAppendString( Buf2, "ABC" );
VBShiftRight(Buf2, 0, '>' );
assert( VBSize(Buf2) == 3 );
assert( !strcmp(VBAsString(Buf2), "ABC"));
}
#define TEMPFILENAME "/tmp/VarBufTest.tmp"
static void TestReadLine( VarBuf Buf1, VarBuf Buf2 )
{
StandardIO Null = SIOCreate();
StandardIO Temp = SIOCreate();
VBReset( Buf2 );
SIOFileOpen( Null, "/dev/null", OpenModeRead );
assert( Null != NULL );
assert( !VBReadLine( Buf2, Null, True ) );
assert( VBSize(Buf2) == 0 );
SIOFileClose( Null );
SIOFileOpen( Temp, TEMPFILENAME, OpenModeWriteDiscardOld );
SIOPrintf( Temp, "\n" );
SIOFileClose( Temp );
SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
VBReset( Buf1 );
VBReadLine( Buf1, Temp, True );
assert( VBSize( Buf1 ) == 1 );
assert( !strcmp( VBAsString(Buf1), "\n" ) );
SIOFileClose( Temp );
SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
VBReset( Buf1 );
assert( !strcmp( VBAsString(Buf1), "" ) );
VBReadLine( Buf1, Temp, False );
assert( VBSize( Buf1 ) == 0 );
assert( !strcmp( VBAsString(Buf1), "" ) );
assert( VBAsString(Buf1) != NULL );
assert( !strcmp( VBAsString(Buf1), "" ) );
SIOFileClose( Temp );
SIOFileOpen( Temp, TEMPFILENAME, OpenModeWriteDiscardOld );
SIOPrintf(Temp, "%s", "alt.arts.nomad 0000000757 0000000554 y\n");
SIOPrintf(Temp, "%s", "alt.ascii-art 0000023551 21588 y\n" );
SIOPrintf(Temp, "%s", "alt.ascii-art.animation 0000001209 01109 y\n" );
SIOPrintf(Temp, "%s", "alt.asian-movies 0000016485 0000013889 y\n" );
SIOFileClose( Temp );
SIOFileOpen( Temp, TEMPFILENAME, OpenModeRead );
VBReset( Buf1 );
VBReadLine( Buf1, Temp, True );
assert( !strcmp(VBAsString(Buf1), "alt.arts.nomad 0000000757 0000000554 y\n" ) );
VBReset( Buf1 );
VBReadLine( Buf1, Temp, True );
assert( !strcmp(VBAsString(Buf1), "alt.ascii-art 0000023551 21588 y\n" ));
VBReset( Buf1 );
VBReadLine( Buf1, Temp, True );
assert( !strcmp(VBAsString(Buf1), "alt.ascii-art.animation 0000001209 01109 y\n"));
VBReset( Buf1 );
VBReadLine( Buf1, Temp, True );
assert( !strcmp(VBAsString(Buf1), "alt.asian-movies 0000016485 0000013889 y\n"));
SIOFileClose( Temp );
VBReset( Buf1 );
VBPrintf( Buf1, "A%cle %s %s in %she%c %s", 'l', "eendjes", "zwemmen", "",
't', "water" );
assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
VBReset( Buf1 );
VBReset( Buf2 );
VBPrintf( Buf2, "zwemmen in" );
VBPrintf( Buf1, "Alle %s %V het %s", "eendjes", Buf2, "water" );
assert( !strcmp( VBAsString(Buf1), "Alle eendjes zwemmen in het water" ) );
system( "rm -f /tmp/VarBufTest.tmp" );
SIODestroy( Null );
SIODestroy( Temp );
}
static void TestVBFixSize( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBReset( Buf2 );
VBFixSize( Buf1, 6, 'X' );
assert( !strcmp( VBAsString( Buf1 ), "XXXXXX" ) );
VBFixSize( Buf1, 2, 'Y' );
assert( !strcmp( VBAsString( Buf1 ), "XX" ) );
VBFixSize( Buf1, 4, 'Z' );
assert( !strcmp( VBAsString( Buf1 ), "XXZZ" ) );
VBFixSize( Buf1, 0, 'Z' );
assert( VBSize( Buf1 ) == 0 );
VBFixSize( Buf1, 10, 'X' );
VBFixSize( Buf1, 10, 'X' );
assert( !strcmp( VBAsString( Buf1 ), "XXXXXXXXXX" ) );
}
static void TestVBRemoveLeadingBlanks( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBReset( Buf2 );
VBRemoveLeadingBlanks( Buf1 );
assert( VBSize( Buf1 ) == 0 );
VBAppendString( Buf1, " \t\n\f\r\v" );
VBRemoveLeadingBlanks( Buf1 );
assert( VBSize( Buf1 ) == 0 );
VBAppendString( Buf1, " XYZ " );
VBRemoveLeadingBlanks( Buf1 );
assert( !strcmp( VBAsString( Buf1 ), "XYZ " ) );
}
static void TestVBRemoveTrailingBlanks( VarBuf Buf1, VarBuf Buf2 )
{
VBReset( Buf1 );
VBReset( Buf2 );
VBRemoveTrailingBlanks( Buf1 );
assert( VBSize( Buf1 ) == 0 );
VBAppendString( Buf1, " \t\n\f\r" );
VBRemoveTrailingBlanks( Buf1 );
assert( VBSize( Buf1 ) == 0 );
VBAppendString( Buf1, " XYZ " );
VBRemoveTrailingBlanks( Buf1 );
assert( !strcmp( VBAsString(Buf1), " XYZ" ) );
}
static void TestSplit( VarBuf Buf1, VarBuf Buf2 )
{
VarBuf Buf3 = VBCreate();
const char * SplitString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
VBAppendString( Buf3, SplitString );
for ( i = 0; i < strlen(SplitString); i++ )
{
VBReset( Buf1 );
VBReset( Buf2 );
VBSplitAtOffset( Buf3, i, Buf1, Buf2 );
assert( !strcmp( VBAsString(Buf3), SplitString ) );
assert( VBSize(Buf1) == i );
assert( !strncmp( VBAsString(Buf1), SplitString, i ) );
assert( VBSize(Buf2) == ( strlen(SplitString) - i ) );
assert( !strcmp( VBAsString(Buf2), SplitString + i ) );
}
VBDestroy( Buf3 );
}
static int MyCompare( VarBuf Buf1, VarBuf Buf2 )
{
return strcmp( VBAsString(Buf1), VBAsString(Buf2) );
}
static void TestSort( VarBuf Buf1, VarBuf Buf2 )
{
VarBuf * p;
VarBuf A = VBCreate();
VarBuf B = VBCreate();
VarBuf C = VBCreate();
VarBuf D = VBCreate();
VarBuf E = VBCreate();
VarBuf F = VBCreate();
VarBuf Array = VBCreate();
VBAppendString( A, "ABCDE" );
VBAppendString( B, "BCDEF" );
VBAppendString( C, "CDEFG" );
VBAppendString( D, "DEFGH" );
VBAppendString( E, "EFGHI" );
VBAppendString( F, "JKLMN" );
VBAppendVBReference( Array, D );
VBAppendVBReference( Array, A );
VBAppendVBReference( Array, B );
VBAppendVBReference( Array, F );
VBAppendVBReference( Array, E );
VBAppendVBReference( Array, C );
VBSortArrayOfVB( Array, MyCompare );
p = (VarBuf *)VBAddress(Array);
assert( p[0] == A );
assert( p[1] == B );
assert( p[2] == C );
assert( p[3] == D );
assert( p[4] == E );
assert( p[5] == F );
VBDestroy(A);
VBDestroy(B);
VBDestroy(C);
VBDestroy(D);
VBDestroy(E);
VBDestroy(F);
VBDestroy(Array);
}
int main( void )
{
VarBuf Buf1;
VarBuf Buf2;
TestCreation( &Buf1, &Buf2 );
TestSort( Buf1, Buf2 );
TestSplit( Buf1, Buf2 );
TestAppendStringAndChar( Buf1, Buf2 );
TestManyAppends( Buf1, Buf2 );
TestTruncate( Buf1, Buf2 );
TestReadLine( Buf1, Buf2 );
TestShiftLeft( Buf1, Buf2 );
TestShiftRight( Buf1, Buf2 );
TestVBFixSize( Buf1, Buf2 );
TestVBRemoveLeadingBlanks( Buf1, Buf2 );
TestVBRemoveTrailingBlanks( Buf1, Buf2 );
VBDestroy( Buf1 );
VBDestroy( Buf2 );
SIOPrintf( StandardOutput, "OK\n" );
return EXIT_SUCCESS;
}